import random

irc = None
assrape_list = []

def init(bot):
    global irc
    irc = bot
    irc.cmd.events['PRIVMSG'].subscribe(handle_privmsg)
    load_list()

    print "assrape.py v2.0 by Matt@#liek loaded."

def handle_privmsg(sender, args):
    global irc, assrape_list

    msg = args[0].split(None, 2)
    if msg:
    	if msg[0].lower() == "~assrape":
            if len(msg) > 2 and msg[1].lower() == '-add':
                if irc.chmgr.get(args[2]).isop(sender.nick):
                    found = False
                    msg[2] = msg[2].rstrip()
                    if msg[2][0] == ':' and len(msg[2]) < 2:
                        irc.privmsg(args[2], "I can't do that.")
                    item = msg[2].lower()
                    for i in assrape_list:
                        if i.lower() == item:
                            irc.privmsg(args[2], 
                                        "I've already got one of those.")
                            found = True
                    if not found:
                        add_list(msg[2])
                        irc.privmsg(args[2], '"%s" added.' % (msg[2]))
            elif len(msg) > 2 and msg[1].lower() == '-rem':
                if irc.chmgr.get(args[2]).isop(sender.nick):
                    found = False
                    if not assrape_list:
                        return
                    item = msg[2].lower()
                    for x in range(len(assrape_list)):
                        if assrape_list[x].lower() == item:
                            assrape_list.pop(x)
                            save_list()
                            found = True
                            irc.privmsg(args[2], '"%s" Removed.' % (msg[2]))
                            break
                    if not found:
                        irc.privmsg(args[2], "I couldn't find that.")
            elif len(msg) > 1 and msg[1].lower() == '-reload':
                if irc.chmgr.get(args[2]).isop(sender.nick):
                    load_list()
                    irc.privmsg(args[2], "Reloaded assrape list.")
            elif len(msg) > 1:
                nick = msg[1]
                if irc.irccmp(irc.nickname, nick) == 0:
                    irc.privmsg(args[2], "Nice try, asshole.")
                    irc.kick(args[2], sender.nick, "Assraped")
                elif irc.chmgr.get(args[2]).isop(sender.nick):
                    dec_nick = ""
                    for u in irc.chmgr.get(args[2]).users:
                        if irc.irccmp(u, nick) == 0:
                            dec_nick = u
                            break
                    if dec_nick:
                        item = None
                        try:
                            item = random.choice(assrape_list)
                        except IndexError:
                            pass
                        if not item:
                            item = ':something'
                        assrape_with = ""
                        if item[0] != ':':
                            assrape_with = 'an ' if item[0].lower() in 'aeiou' else 'a '
                            assrape_with += item
                            irc.action(
                                args[2],
                                "picks up %s and shoves it up %s's ass." %
                                (assrape_with,
                                 dec_nick))
                        else:
                            assrape_with = item[1:]
                            irc.action(
                                args[2],
                                "grabs %s and shoves it up %s's ass." %
                                (assrape_with,
                                 dec_nick))
                        irc.kick(args[2], dec_nick, "Assraped with %s" % (assrape_with))
            else:
                irc.notice(sender.nick, "Syntax: ~assrape <nick>")
                if irc.chmgr.get(args[2]).isop(sender.nick):
                    irc.notice(sender.nick, "        ~assrape -add [:]<item>")
                    irc.notice(sender.nick, "        ~assrape -rem [:]<item>")
                        
def load_list():
    global assrape_list
    assrape_list = []
    try:
        with open('assrape.txt', 'r') as f:
            assrape_list = f.read().splitlines()
    except:
        pass

def add_list(item):
    global assrape_list
    assrape_list.append(item)
    try:
        with open('assrape.txt', 'a') as f:
            print >>f, item
    except:
        pass

def save_list():
    try:
        with open('assrape.txt', 'w') as f:
            for i in assrape_list:
                print >>f, i
    except:
        pass
